// components/choosePlayer/choosePlayer.js




Component({
  /**
   * 组件的属性列表
   */
  properties: {
    // 联系人列表
    contactList:{
      type:'Array',
      value:[]
    },
    // 票种信息
    ticketInfo:{
      type:'Object',
      value:{}
    },
    // 活动ID
    activityId: {
      type: null
    },
    // 票种ID
    ticketId: {
      type: null
    },

    
  },

  /**
   * 组件的初始数据
   */
  data: {
    chooseArr: [],   //已选择的联系人id数组
  },
  ready:function(){     //
    this.init();
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 初始化选择框
    init:function(){
      //console.log(this.properties.contactList);
      // 计算已选择联系人id数组
      let chooseArr = [];
      this.properties.contactList.forEach(function (item, index) {
        if (item.checked) {
          chooseArr.push(item.id);
        }
      });
      this.setData({
        chooseArr
      });
    },
    close: function() {
      var myEventDetail = {} // detail对象，提供给事件监听函数
      var myEventOption = {} // 触发事件的选项
      this.triggerEvent('close', myEventDetail, myEventOption)
    },
    ok: function() {
      let ticketNum = this.data.ticketInfo.ticketNum;
      let contactList = this.data.contactList;
      let chooseArr = this.data.chooseArr;
      if (chooseArr.length != ticketNum){  //判断联系人数量是否符合
        wx.showToast({
          title: '请选择' + ticketNum + '位联系人',
          icon:'none'
        });
        return;
      };

      var myEventDetail = { // detail对象，提供给事件监听函数
        contactList,
        chooseArr
      };
      var myEventOption = {};
      this.triggerEvent('ok', myEventDetail, myEventOption)
    },
    // 新增联系人
    addContact:function(){
      let contactList = this.properties.contactList;
      let activityId = this.properties.activityId;
      let ticketId = this.properties.ticketId;
      if (contactList.length==0){   //新增第一个联系人，添加时默认用户基本信息
        wx.navigateTo({
          url: '/pages/addContact/addContact?pageType=addFirst&activityId=' + activityId + '&ticketId=' + ticketId
        })
      }else{
        wx.navigateTo({
          url: '/pages/addContact/addContact?pageType=add&activityId=' + activityId + '&ticketId=' + ticketId
        })
      }
    },
    // 选择多选框
    checkboxChange: function(e) {
      //console.log(e)
      let chooseArr = e.detail.value;
      if (this.checkChooseNum(e.detail.value)) { // 人数没有超限
        this.setData({
          chooseArr
        });
      };
      this.setChooseData();
    },
    // 编辑联系人
    editContact: function(e) {
      // //console.log('编辑'+e.currentTarget.dataset.id);
      let id = e.currentTarget.dataset.id;
      let activityId = this.properties.activityId;
      let ticketId = this.properties.ticketId;
      wx.navigateTo({
        url: '/pages/addContact/addContact?pageType=edit&contactId=' + id + '&activityId=' + activityId + '&ticketId=' + ticketId,
      })
    },
    // 校验最后选中的人信息是否齐全、选择人数是否没超限
    checkChooseNum:function(arr){
      if(arr.length == 0){return true};
      let ticketNum = this.data.ticketInfo.ticketNum;
      let contactList = this.data.contactList;
      let lastItem = contactList.find(function (item) {     //最后选中的联系人
        return item.id == arr[arr.length - 1];
      });
      let activityId = this.properties.activityId;
      let ticketId = this.properties.ticketId;
      if (arr.length > ticketNum) {  // 人数超限，阻止
        wx.showToast({
          title: '请选择'+ticketNum+'位联系人',
          icon:'none'
        });
        return false;
      }else{
        if (lastItem.infoEnough){    //信息完整，放行
          return true;
        }else{    //信息不完整，阻止并调整
          wx.navigateTo({
            url: '/pages/addContact/addContact?pageType=edit&contactId=' + lastItem.id + '&activityId=' + activityId + '&ticketId=' + ticketId,
          })
          return false;
        }
      }
    },
    // 设置多选框数据
    setChooseData:function(){
      let chooseArr = this.data.chooseArr;
      let contactList = this.data.contactList;
      contactList.forEach(function(itemA,indexA){
        let itemChecked = false;
        chooseArr.forEach(function(itemB,indexB){
          if(itemA.id == itemB ){
            itemChecked = true;
          }
        });
        contactList[indexA].checked = itemChecked;
      });
      this.setData({
        contactList
      });
    },

  }
})